<template>
    <div class="system-offer-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <el-card shadow="hover" class="layout-padding-auto">
                <div class="table-search-container">
                    <el-form ref="tableSearchRef" :model="state.ruleForm" size="default" label-width="100px"
                        class="table-form">
                        <el-row>



                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
                                <el-form-item label="年份">
                                    <el-select v-model="state.ruleForm.year" placeholder="请选择" clearable
                                        style="width: 100%">
                                        <el-option v-for="i in state.yearList" :key="i.value" :label="i.name"
                                            :value="i.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>




                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="账户地址">
                                    <el-input v-model="state.ruleForm.address" style="width: 100%"
                                        placeholder="账户地址"></el-input>
                                </el-form-item>
                            </el-col>





                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="操作类型">
                                    <el-select v-model="state.ruleForm.tradeType" placeholder="请选择" clearable
                                        style="width: 100%">
                                        <el-option v-for="i in state.typeList" :key="i.value" :label="i.name"
                                            :value="i.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>


                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="状态">
                                    <el-select v-model="state.ruleForm.status" placeholder="请选择" clearable
                                        style="width: 100%">
                                        <el-option label="审核不通过" value="-5"></el-option>
                                        <el-option label="错误" value="-30"></el-option>
                                        <el-option label="审核中" value="5"></el-option>
                                        <el-option label="结算中" value="10"></el-option>
                                        <el-option label="完成" value="20"></el-option>
                                        <el-option label="待确认" value="40"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>




                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="币种">
                                    <el-select v-model="state.ruleForm.coinId" placeholder="请选择" clearable
                                        style="width: 100%">
                                        <el-option v-for="i in state.coinList" :key="i.coinId" :value="i.coinId"
                                            :label="i.coinSymbol">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>



                            <el-col :xs="24" :sm="12" :md="9" :lg="7" :xl="5" class="mb20" v-show="state.isToggle">
                                <el-form-item label="创建时间">
                                    <el-date-picker v-model="state.ruleForm.createDate" type="datetimerange"
                                        :picker-options="state.pickerOptions" range-separator="至" start-placeholder="开始日期"
                                        end-placeholder="结束日期" align="right" style="width: 100%">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>


                            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                                <el-form-item class="table-form-btn" :label-width="!state.isToggle ? '10px' : '35px'">
                                    <div class="table-form-btn-toggle ml10" @click="state.isToggle = !state.isToggle">
                                        <span>{{ state.isToggle ? '收筛选' : '展筛选' }}</span>
                                        <SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />
                                    </div>
                                    <div>
                                        <el-button size="default" type="primary" @click="refresh()">查询
                                        </el-button>
                                        <el-button size="default" type="info" class="ml10" @click="handleResetSearch()"> 重置
                                        </el-button>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>


                <el-table size="default" :data="state.dataList" style="width: 100%" border="">

                    <el-table-column align="center" label="id" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.addressAccountDetailId }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="操作地址">
                        <template #default="scope">
                            <span>{{ scope.row.address }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="币种" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.coinSymbol }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="币种Id" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.coinId }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="Hash">
                        <template #default="scope">
                            <span>{{ scope.row.transHash }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="操作金额" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.amount }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="操作类型" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.tradeTypeStr }} </span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="描述">
                        <template #default="scope">
                            <span>{{ scope.row.remark || "--" }} </span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="状态">
                        <template #default="scope">
                            <span>{{ scope.row.statusStr || "--" }} </span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="创建时间" sortable="custom" prop="CREATED_DATE" width="160">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.createdDate) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="更新时间" width="160" prop="update_Date">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.updatedDate) }}</span>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>

            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                :pager-count="5" :page-sizes="[20, 50, 100]" v-model:current-page="state.page" background
                v-model:page-size="state.limit" layout="total, sizes, prev, pager, next, jumper" :total="state.total">
            </el-pagination>
        </el-card>

    </div>
</template>

<script setup lang="ts" name="systemMenu">
import { formatDate } from '/@/utils/formatTime';
import { onMounted, reactive, defineAsyncComponent, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';




import { useFundApi } from '/@/api/fund/index'






const state = reactive({
    isToggle: false,
    page: 1,
    limit: 20,
    total: 0,

    yearList: [] as any,


    typeList: [] as any,
    dataList: [] as any,
    coinList: [] as any,
    systemTypeSelect: [] as any,// 系统账户类型
    ruleForm: {
        year: null,
        address: "",
        status: "",
        tradeType: "",
        coinId: "",
        createDate: []
    },
    pickerOptions: {
        shortcuts: [
            {
                text: "最近一周",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 7
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近一个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 30
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近三个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 90
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
        ],
    }

});


const refresh = () => {
    queryAccountDetail();
}



const handleResetSearch = () => {
    state.ruleForm = {
        year: new Date().getFullYear(),
        address: "",
        status: "",
        tradeType: "",
        coinId: "",
        createDate: []
    };
    state.page = 1;
    state.limit = 20;
    queryAccountDetail();
}


const formatDate2 = (str: any) => {
    if (!str) return "-";
    return formatDate(new Date(str), "YYYY-mm-dd HH:MM")
}


// 分页改变
const onHandleSizeChange = (val: number) => {
    state.limit = val;
    queryAccountDetail();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.page = val;
    queryAccountDetail();
};

const getUserLevStr = (row: any) => {
    let str = row;
    state.systemTypeSelect.forEach((element: any) => {
        if (element.value == row) {
            str = element.name;
        }
    });
    return str;
}


const queryAccountDetail = async () => {
    let vo = {
        // page: state.page,
        // limit: state.limit
    } as any;


    if (state.ruleForm.address) vo["address"] = state.ruleForm.address;
    if (state.ruleForm.status) vo["status"] = state.ruleForm.status;
    if (state.ruleForm.coinId) vo["coinId"] = state.ruleForm.coinId;

    if (state.ruleForm.tradeType) vo["tradeType"] = state.ruleForm.tradeType;

    if (state.ruleForm.year) vo["year"] = state.ruleForm.year;

    let startTime, endTime;
    if (state.ruleForm.createDate && state.ruleForm.createDate.length > 0) {
        startTime = state.ruleForm.createDate[0];
        endTime = state.ruleForm.createDate[1];
    }
    if (startTime) vo["startTime"] = startTime;
    if (endTime) vo["endTime"] = endTime;


    let listTemp = await useFundApi().queryAccountDetailHistory(state.page, state.limit, vo);





    let tempList = [] as any;
    let obj = {
        "-5": "审核不通过",
        "-30": "错误",
        5: "审核中",
        10: "结算中",
        20: "完成",
        40: "待确认",
    };

    listTemp.list.forEach((element: any) => {
        state.typeList.forEach((el: any) => {
            if (el.value == element.tradeType) {
                element.tradeTypeStr = el.name || "--";
                element.statusStr =
                    obj[element.status.toString()] || "--";
            }
        });
        element.amount = element.amount + " " + element.coinSymbol;
        tempList.push(element);
    });













    state.dataList = tempList;
    state.total = listTemp.total;
}





const getcoinList = async () => {
    let listTemp = await useFundApi().getcoinList();
    state.coinList = listTemp;
}



const getTradeTypes = async () => {
    let listTemp = await useFundApi().getTradeTypes();
    state.typeList = listTemp;
}


const initYearList = async () => {
    let listTemp = [] as any;
    let nowDate = new Date();
    for (let i = 0; i < 5; i++) {
        let date = nowDate.getFullYear() - i;
        listTemp.push({
            name: date,
            value: date,
        });
    }
    state.yearList = listTemp;
    state.ruleForm.year = new Date().getFullYear();
}


// 页面加载时
onMounted(() => {
    initYearList();

    getTradeTypes();
    getcoinList();
    queryAccountDetail();
});





</script>



<style scoped lang="scss">
.bar_tools {
    display: flex;
    flex-direction: row-reverse;
    margin-bottom: 10px;
}

.system-offer-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}


.table-search-container {
    display: flex;

    .table-form {
        flex: 1;

        .table-form-btn-toggle {
            white-space: nowrap;
            user-select: none;
            display: flex;
            align-items: center;
            color: var(--el-color-primary);
            margin-right: 15px;
        }
    }
}
</style>

